#ifndef _LOCKER_H_
#define _LOCKER_H_

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#ifdef __cplusplus
extern "C"{
#endif

typedef struct locker locker_t;
typedef int (*lock_func)(locker_t *self);
typedef int (*unlock_func)(locker_t *self);
typedef void (*destroy_func)(locker_t *self);
typedef int (*get_func)(locker_t *self, void *buf, size_t size);


struct locker
{
    lock_func lock;
    unlock_func unlock;
    destroy_func destroy;
    get_func get;
    char priv[0];
};

static inline int locker_lock(locker_t *self)
{
    if(self && self->lock){
        return (*self->lock)(self);
    }

    return 0;
}

static inline int locker_unlock(locker_t *self)
{
    if(self && self->unlock){
        return (*self->unlock)(self);
    }

    return 0;
}

static inline void locker_destroy(locker_t *self)
{
    if(self && self->destroy){
        return (*self->destroy)(self);
    }
}

static inline int locker_get(locker_t *self, void *buf, size_t size)
{
    if(self && self->get){
        return (*self->get)(self, buf, size);
    }

    return -1;
}


#ifdef __cplusplus
}
#endif

#endif
